package sg.edu.nus.iss.vmcs.store;

import sg.edu.nus.iss.vmcs.customer.StoreMemento;

/*
 * Copyright 2011 ISS.
 * The contents contained in this document may not be reproduced in any
 * form or by any means, without the written permission of ISS, other
 * than for the purpose for which it has been supplied.
 *
 */

/**
 * This entity object implements a generic Store (an abstract class). It has 
 * operations to load (add) Store Items into the Store and release Store Items 
 * from the Store. 
 * @version 1.0  07/12/2011
 * @author Team 7S - SE19
 */
public abstract class Store {

    /**
     * CASH means it is a Cash Store.
     */
	public final static int CASH  = 1;
        /**
         * DRINK means it is a Drink Store.
         */
	public final static int DRINK = 2;

        /**
         * the size of the store.
         */
	protected int size;

        /**
         * an array of Store items.
         */
	protected StoreItem items[];

        /**
         * This constructor creates a new instance of Store.  
         */
	public Store() {
	}

        /**
         * This constructor creates a new instance of a Store of a particular
         * size.
         * @param itemn the size of the store.
         */
	public Store(int itemn) {
		size = itemn;
		items = new StoreItem[size];
	}

        /**
         * This operation sets the size of the items array in the Store. 
         * @param sz the size of the store.
         */
	public void setStoreSize(int sz) {
		size = sz;
		items = new StoreItem[size];
	}

        /**
         * This operation gets the items in a store.
         * @return returns an array containing the items of a store.
         */
	public StoreItem[] getItems() {
		return items;
	}

        /**
         * This operation adds store items into the store. 
         * @param idx the index that represents the item.
         * @param object the store item object that is to be added to the store.
         */
	public void addItem(int idx, StoreItem object) {
		if ((idx >= size) || (idx < 0))
			return;
		items[idx] = object;
	}

        /**
         * This operation returns the Store Item corresponding to the index 
         * entered. 
         * @param idx the index that represents the Store Item.
         * @return returns the Store Item.
         */
	public StoreItem getStoreItem(int idx) {
		if ((idx >= size) || (idx < 0))
            return null;
		return items[idx];
	}

        /**
         * This operation finds a Store Object in the store with a specified 
         * name. 
         * @param name the name of the Store Object.
         * @return returns the Store Object. Null if the Store Object does not
         * exists.
         */
	public StoreObject findObject(String name) {
		String en;
		StoreObject so;
		int i;

		for (i = 0; i < size; i++) {
			if (items[i] == null)
				return null;
			so = items[i].getContent();
			if (so == null)
				return null;
			en = so.getName();
			if (en != null) {
				if (en.compareTo(name) == 0)
					return so;
			}
		}
		return null;
	}

        /**
         * This operation sets the total number of a store item held. 
         * @param idx the index that represents the store item.
         * @param qty the quantity of the store item to be set.
         */
	public void setQuantity(int idx, int qty) {
		//System.out.println("Store: setQauntity- qty=" + qty);
		if ((idx >= size) || (idx < 0))
			return;
		items[idx].setQuantity(qty);
	}
/**
 * This operations returns the size of the store which represents the number of 
 * items held in the store.
 * @return the integer value of the size of the store.
 */
	public int getStoreSize() {
		return size;
	}
	
	/**
	 * This operations  restore the items from StoreMemento
	 * @return the integer value of the size of the store.
	 */

	public void setMemento(StoreMemento memento ){
		System.out.println("Calling setMemento");
		this.items = memento.getItems();
		
	}
	
	/**
	 * This operations create  the Memento  of items for later retrieval .
	 * @return the integer value of the size of the store.
	 */
	public StoreMemento createMemento(){
		System.out.println("Calling createMemento...");
		return  new StoreMemento(this.items);
	}

}
